﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using SmartCity.ProcessEngine.API.Write.Application.CommandHandlers.FormNodes;
using SmartCity.ProcessEngine.Domain;
using SmartCity.ProcessEngine.Errorcode;
using SmartCity.ProcessEngine.Infrastructure.Interfaces;

namespace SmartCity.ProcessEngine.API.Write.Application.Commands.FormNodes
{
    /// <summary>
    /// 处理
    /// </summary>
    public class DeleteNodeCommandHandler:IRequestHandler<DeleteNodeCommand,ApiResult<int>>
    {
        private readonly IBaseRepository<WF_Node> nodeRep;

        public DeleteNodeCommandHandler(IBaseRepository<WF_Node> nodeRep)
        {
            this.nodeRep = nodeRep;
        }
        /// <summary>
        /// 删除节点处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回删除节点任务</returns>
        public async Task<ApiResult<int>> Handle(DeleteNodeCommand request, CancellationToken cancellationToken)
        {
            var list = await nodeRep.FindAsync(request.NodeId);
            list.IsDeleted = true;
            var res = await nodeRep.UpdateAsync(list);
            return new ApiResult<int>
            {
                code = ApiEnum.DeleteSuccess,
                message = "删除节点成功",
                data = res
            };
        }
    }
}
